﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SurvivalStrategy.Modules;
using SurvivalStrategy.Modules.ActionArgs;
using SurvivalStrategy.Modules.BehaviorModules;
using Microsoft.Xna.Framework;
using SurvivalStrategy.Entities.ConversationData;
using SurvivalStrategy.Graphics;

namespace SurvivalStrategy
{
    //glowna klasa gry, w ona zarzadza wszystkimi modulami, pobiera wejscia, dokonuje symulacji, wyrzuca wynik do widoku
    public class Model
    {
        private int id;
        
        private Entity clientEntitiy;
        private ConversationManager converstationManager;

        private EntitiesManager entitiesManager;
        private DataBaseManager dataBaseManager;

        public World world { get; internal set; }

        private List<GraphicElement> graphicsData;
        public List<GraphicElement> GraphicsData { get { return graphicsData; } }

        public Model()
        {
            id = 0;

            entitiesManager = new EntitiesManager();
            converstationManager = new ConversationManager();
        }

        public void LoadWorld(World world)
        {
            this.world = world;
        }

        public void Update(GameTime gameTime)
        {
            entitiesManager.Update();
            converstationManager.Update();
            world.Update();
            GetGraphicsData();
        }

        private void GetGraphicsData()
        {
            graphicsData = new List<GraphicElement>(); 
            graphicsData.Clear();
            GetEntietiesGraphicData();
            GetWorldGraphicsData();
        }

        private void GetEntietiesGraphicData()
        {
            foreach (Entity en in entitiesManager.Entities.Values)
                graphicsData.Add(new Graphics.GraphicElement(en.WorldInfo.Appearance, new Vector3(en.WorldInfo.Position.X-16,en.WorldInfo.Position.Y,en.WorldInfo.Position.Z-16)));
        }

        private void GetWorldGraphicsData()
        {
            List<WorldData.WorldChunk> chunks = new List<WorldData.WorldChunk>();
            chunks.Add(world.GetPositionsChunk(clientEntitiy.WorldInfo.Position));
            
            foreach (WorldData.WorldChunk c in world.Chunks) // potem chunks
                foreach (WorldData.WorldElement els in c.ChunkElements.Values)
                {
                    Graphics.GraphicElement element = new Graphics.GraphicElement(els.TextureID, els.Position);
                    graphicsData.Add(element);
                }
        }

        public void AddEntity()
        {
            Entity en = new Entity(GetNextID());
            en.Initialize();
            en.SetController(new PlayerController());
            en.WorldInfo.Position = new Vector3(50, 50, 50);
            en.WorldInfo.Appearance = 2;
            //en.AddBehaviorModule(new Modules.BehaviorModules.StandardMovingBehaviorModule());
            en.AddActionModule(new Modules.ActionModules.StandardMovingModule());
            en.AddActionModule(new Modules.ActionModules.GetItemModule());
            en.AddComponent<Entities.EnitityComponents.EntitiyCollider>();
            en.GetComponent<Entities.EnitityComponents.EntitiyCollider>().SetCollider(new WorldData.Colliders.RectangleCollider(Vector2.Zero, new Vector2(32, 32)));
            en.AddComponent<Entities.EnitityComponents.SurvivalComponent>();
            en.GetComponent<Entities.EnitityComponents.SurvivalComponent>().Energy = 20;
            en.AddComponent<Entities.EnitityComponents.EntityItemContainter>();
            en.GetComponent<Entities.EnitityComponents.EntityItemContainter>().Capacity = 5;
            entitiesManager.AddEntity(en);
            SetClientEntity(en.ID);



            Entity en1 = new Entity(GetNextID());
            en1.Initialize();
            en1.SetController(new AgentController());
            en1.WorldInfo.Position = new Vector3(100, 50, 50);
            en1.WorldInfo.Appearance = 2;
            en1.AddBehaviorModule(new Modules.BehaviorModules.StandardMovingBehaviorModule());
            en1.AddActionModule(new Modules.ActionModules.StandardMovingModule());
            en1.AddActionModule(new Modules.ActionModules.GetItemModule());
            en1.AddComponent<Entities.EnitityComponents.EntitiyCollider>();
            en1.GetComponent<Entities.EnitityComponents.EntitiyCollider>().SetCollider(new WorldData.Colliders.RectangleCollider(Vector2.Zero, new Vector2(32, 32)));
            en1.AddComponent<Entities.EnitityComponents.SurvivalComponent>();
            en1.GetComponent<Entities.EnitityComponents.SurvivalComponent>().Energy = 20;
            en1.AddComponent<Entities.EnitityComponents.EntityItemContainter>();
            en1.GetComponent<Entities.EnitityComponents.EntityItemContainter>().Capacity = 5;
            entitiesManager.AddEntity(en1);
            SetClientEntity(en1.ID);
        }

        public int GetNextID()
        {
            int result = id;
            id++;
            return result;
        }

        public void SetClientEntity(int id)
        {
            clientEntitiy = entitiesManager.GetEntity(id);
        }   
    }
}
